package com.smartstate.iot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartstate.iot.entity.TopicConfig;
import com.smartstate.iot.entity.vo.GatewayInfoVo;
import com.smartstate.iot.vo.TopicUpVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


@Mapper
public interface TopicMapper extends BaseMapper<TopicConfig> {

    /**
     * 查询当前IP端口数量
     * @param content
     * @return
     */
    @Select("SELECT COUNT(*) FROM topic_config WHERE ip = #{ip} and port = #{port}")
    Integer getIpPort(TopicUpVo.Content content);

    /**
     * 更新指定IP端口盒子心跳信息
     * @param topicConfig
     * @param content
     * @return
     */
    @Update("update topic_config set deviceId =#{topicConfig.deviceId} , deviceType=#{topicConfig.deviceType}, deviceName=#{topicConfig.deviceName},config=#{topicConfig.config},newTopic =#{topicConfig.newTopic},timesTamps=#{topicConfig.timesTamps},cpu=#{topicConfig.cpu},momery=#{topicConfig.momery} where ip = #{content.ip} and port = #{content.port}")
    Boolean updateByPortIp(TopicConfig topicConfig, TopicUpVo.Content content);

    /**
     * 获取网关cpu
     * @param deviceName
     * @return
     */
    @Select("SELECT cpu FROM topic_config x WHERE timesTamps = ( SELECT MAX(timesTamps) FROM topic_config y WHERE x.deviceId = y.deviceId ) AND deviceId = #{deviceName}; ")
    Integer getCpuInfo(String deviceName);

    /**
     * 获取网关内存
     * @param deviceName
     * @return
     */
    @Select("SELECT momery FROM topic_config x WHERE timesTamps = ( SELECT MAX(timesTamps) FROM topic_config y WHERE x.deviceId = y.deviceId ) AND deviceId = #{deviceName}; ")
    Integer getMemory(String deviceName);

    /**
     * 获取网关列表信息
     * @return
     */
//    @Select("SELECT temp.type deviceStatus,c.deviceName deviceName,c.ip deviceIp,c.`port` devicePort FROM (SELECT * FROM topic_log x  WHERE x.date = ( SELECT MAX(date) FROM topic_log x1 WHERE x.deviceId = x1.deviceId )) temp ,(SELECT * FROM topic_config x  WHERE x.timesTamps = ( SELECT MAX(timesTamps) FROM topic_config x1 WHERE x.deviceId = x1.deviceId )) c WHERE temp.deviceId = c.deviceId;")
//    List<GatewayInfoVo> getBoxInfo();
    @Select("SELECT log.type deviceStatus,config.deviceName,config.ip deviceIp,config.`port` devicePort FROM topic_log log,topic_config config WHERE log.deviceId = config.deviceId GROUP BY log.deviceId ORDER BY log.date DESC;")
    List<GatewayInfoVo> getBoxInfo();

    /**
     * 查询这个当前端口的上次心跳信息时间戳
     */
    @Select("select * from  topic_config  WHERE ip = #{ip} and port = #{port}")
    TopicConfig getUpTime(TopicConfig topicConfig);

    @Select("SELECT topic_config.*  FROM topic_config, ( SELECT temp.deviceId  FROM ( SELECT * FROM topic_log x WHERE x.date = ( SELECT MAX( date ) FROM topic_log x1 WHERE x.deviceId = x1.deviceId )) temp WHERE temp.type = '正常' OR temp.type = '异常' ) log WHERE topic_config.deviceId = log.deviceId;")
    List<TopicConfig> getNormalBoxes();
//    @Select("SELECT config.* FROM topic_config config,( SELECT log.type deviceStatus, config.deviceName, config.ip deviceIp, config.`port` devicePort FROM topic_log log, topic_config config WHERE log.deviceId = config.deviceId GROUP BY log.deviceId ORDER BY log.date DESC ) temp WHERE temp.deviceName = config.deviceName AND (temp.deviceStatus = '正常' OR temp.deviceStatus = '异常');")
//    List<TopicConfig> getNormalBoxes();

    @Select("SELECT * FROM topic_config WHERE deviceId = #{deviceId};")
    TopicConfig getByDeviceId(String deviceId);

    /**
     *  查询网管设备总数
     */
    @Select("select deviceId  from  topic_config ")
    List<String> getAllType();

    @Select("select deviceId from topic_config where deviceName = #{deviceId}")
    String getDeviceIdByDeviceName(String deviceId);
}
